package com.siao.example.common.controller
{
	import com.siao.core.Configuration;
	import com.siao.core.Log;
	import com.siao.data.XmlValue;
	import com.siao.events.BytesEvent;
	import com.siao.example.common.configuration.LibarayDisplayImageConfiguration;
	import com.siao.example.common.configuration.LibarayDisplayTextConfiguration;
	import com.siao.example.common.method.MainMethod;
	import com.siao.loader.BytesLoader;
	import com.siao.loader.BytesQueueLoader;
	import com.siao.mvc.Controller;
	import com.siao.mvc.managers.NoticeManager;
	import com.siao.mvc.notice.Notice;
	
	/**
	 * 启动控制器
	 * @author SiaoLeon
	 */
	public class StartupController extends Controller
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function StartupController()
		{
			super();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		override public function onAdd():void
		{
			
			var loader:BytesLoader  = new BytesLoader();
			loader.load("data/config.xml?v=" + new Date().time);
			loader.addEventListener(BytesEvent.COMPLETE, onConfigListLoadComplete);
			loader.addEventListener(BytesEvent.ERROR, onConifgListLoadError);
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		private function onConfigListLoadComplete(event:BytesEvent):void
		{
			var queueLoader:BytesQueueLoader = new BytesQueueLoader();
			var configs:XML = (event.target as BytesLoader).content;
			
			for (var i:int = 0; i < configs.children().length(); i++)
			{
				var config:XmlValue = new XmlValue(new XML(configs.child(i)));
				queueLoader.load(config.value);
			}
			
			queueLoader.addEventListener(BytesEvent.COMPLETE, onConfigLoadCompllete);
			queueLoader.addEventListener(BytesEvent.ERROR, onConfigLoadError);
		}
		
		private function onConifgListLoadError(event:BytesEvent):void
		{
			Log.error("获取配置列表文件失败！");
		}
		
		private function onConfigLoadCompllete(event:BytesEvent):void
		{
			Configuration.instance.save((event.target as BytesQueueLoader).content as XML);
			
			if ((event.target as BytesQueueLoader).length <= 0)
			{
				Configuration.instance.getConfig("texts").bind(LibarayDisplayTextConfiguration);
				Configuration.instance.getConfig("images").bind(LibarayDisplayImageConfiguration);
				
				NoticeManager.instance.notify(new Notice(null, MainMethod.ENTER_EXAMPLE));
			}
		}
		
		private function onConfigLoadError(event:BytesEvent):void
		{
			Log.error("获取配置文件" + (event.target as BytesQueueLoader).url + "失败！");
		}
	
	}
}
